//
//  XMMacro.h
//  XMKit
//
//  Created by chenming on 2017/12/9.
//  Copyright © 2017年 ChenXiaoMing. All rights reserved.
//

#ifndef XMMacro_h
#define XMMacro_h

/** 屏幕宽度 */
#define kSCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define kSCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height

/** 适配iPhone x 底栏高度 */
#define TabbarHeight ([[UIApplication sharedApplication] statusBarFrame].size.height>20?83:49)

/** 适配iPhone x 导航高度 */
#define NavigationBarHeight ([[UIApplication sharedApplication] statusBarFrame].size.height>20?88:64)

/** 获取系统版本 */
#define iOS_VERSION [[[UIDevice currentDevice] systemVersion] floatValue]

/** 多语言 */
#define XM_LANGUAGE  ([[NSLocale preferredLanguages] objectAtIndex:0])

/** 字符串是否为空 */
#define KSTRING_IS_EMPTY(str) ([str isKindOfClass:[NSNull class]] || str == nil || [str length] < 1 ? YES : NO )

/** 数组是否为空 */
#define KARRAY_IS_EMPTY(array) (array == nil || [array isKindOfClass:[NSNull class]] || array.count == 0)

/** 字典是否为空 */
#define KDICT_IS_EMPTY(dic) (dic == nil || [dic isKindOfClass:[NSNull class]] || dic.allKeys == 0)

/** 是否是空对象 */
#define KOBJECT_IS_EMPYT(_object) (_object == nil \
|| [_object isKindOfClass:[NSNull class]] \
|| ([_object respondsToSelector:@selector(length)] && [(NSData *)_object length] == 0) \
|| ([_object respondsToSelector:@selector(count)] && [(NSArray *)_object count] == 0))

/** keyWindow */
#define KKEY_WINDOW         [UIApplication sharedApplication].keyWindow

/** delegateWindow */
#define KDELEGATE_WINDOW        [UIApplication sharedApplication].delegate.window

/** 日志打印 */
#ifdef DEBUG
#define XMLog(...) printf("[%s] %s [第%d行]: %s\n", __TIME__ ,__PRETTY_FUNCTION__ ,__LINE__, [[NSString stringWithFormat:__VA_ARGS__] UTF8String])
#else
#define XMLog(...)
#endif

/** 弱引用/强引用 */
#define XMWeakSelf(type)  __weak typeof(type) weak##type = type
#define XMStrongSelf(type)  __strong typeof(type) type = weak##type

/**
 Example:
    @weakify(self)
        [self doSomething^{
            @strongify(self)
                if (!self) return;
        ...
        }];
 
 */
#ifndef weakify
    #if DEBUG
        #if __has_feature(objc_arc)
    #define weakify(object) autoreleasepool{} __weak __typeof__(object) weak##_##object = object;
        #else
    #define weakify(object) autoreleasepool{} __block __typeof__(object) block##_##object = object;
#endif
    #else
#if __has_feature(objc_arc)
    #define weakify(object) try{} @finally{} {} __weak __typeof__(object) weak##_##object = object;
    #else
    #define weakify(object) try{} @finally{} {} __block __typeof__(object) block##_##object = object;
        #endif
    #endif
#endif

#ifndef strongify
    #if DEBUG
        #if __has_feature(objc_arc)
    #define strongify(object) autoreleasepool{} __typeof__(object) object = weak##_##object;
        #else
    #define strongify(object) autoreleasepool{} __typeof__(object) object = block##_##object;
    #endif
#else
#if __has_feature(objc_arc)
    #define strongify(object) try{} @finally{} __typeof__(object) object = weak##_##object;
        #else
    #define strongify(object) try{} @finally{} __typeof__(object) object = block##_##object;
        #endif
    #endif
#endif

#endif /* XMMacro_h */
